/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

/**
 * DatabaseUtil类，一个数据库工具类，负责打开和关闭数据库连接
 *
 * @author Lenovo
 */
public class DatabaseUtil {

    private static final String DRIVER_CLASS;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;
    private static final ResourceBundle rb = ResourceBundle.getBundle("com.qdu.util.dbconfig");

    static {
        DRIVER_CLASS = rb.getString("jdbc.driverClass");
        URL = rb.getString("jdbc.url");
        USERNAME = rb.getString("jdbc.username");
        PASSWORD = rb.getString("jdbc.password");
    }

    public static Connection getConnection() {
        Connection con = null;
        try {
            Class.forName(DRIVER_CLASS);
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException | SQLException ex) {
            ex.printStackTrace();
        }
        return con;
    }

    /**
     * @param rs 需要关闭的结果集对象
     * @param stmt 需要关闭的语句对象
     * @param con 需要关闭的连接对象
     *
     * @author Lenovo
     */
    public static void close(ResultSet rs, Statement stmt, Connection con) {

        try {
            if (null != rs) {
                rs.close();
            }
            if (null != stmt) {
                stmt.close();
            }
            if (null != con) {
                con.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
